package com.zb.user.controller;

import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.SelectVo;
import com.zb.result.Result;
import com.zb.service.ZjUserContactService;
import com.zb.service.ZjWorkUserService;
import com.zb.user.service.ConUserService;
import com.zb.utils.TokenUtil;
import com.zb.vo.UserContactNameVo;
import com.zb.vo.UserContactSelectVo;
import com.zb.vo.UserContactVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author WEN
 * @Date 2023/8/10 0010
 **/
@Api(tags = "用户联系人接口")
@RestController
@RequestMapping("/user/contact")
public class ZjUserContactController {
    @Resource
    private ZjUserContactService userContactService;

    @Autowired
    private ConUserService userService;

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "用户档案 - 联系人 - 新增或修改联系人")
    @ApiOperation("新增或修改联系人")
    @PostMapping("/save/{userId}")
    public Result save(HttpServletRequest request, @PathVariable @ApiParam(value = "用户ID", required = true) Long userId, @RequestBody @Valid UserContactVo contactVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        userContactService.save(custAccId, userId, contactVo);
        return Result.success("联系人更新成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.SELECT, remark = "用户档案 - 联系人 - 获取用户联系人列表")
    @ApiOperation("获取用户联系人列表")
    @PostMapping("/getUserContact/{id}")
    public Result getUserContact(@PathVariable @ApiParam(value = "用户ID", required = true) Long id, @RequestBody UserContactSelectVo selectVo) {
        return Result.success("获取用户联系人成功！").data(userContactService.getUserContact(id, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "用户档案 - 联系人 - 更改主要联系人")
    @ApiOperation("更改主要联系人")
    @PutMapping("/{userId}")
    public Result updateMainContact(@PathVariable @ApiParam(value = "用户ID", required = true) Long userId,
                                    @RequestParam @ApiParam(value = "联系人ID", required = true) Long id) {
        userContactService.updateMainContact(userId, id);
        return Result.success("更改主联系人成功！");
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.DELETE, remark = "用户档案 - 联系人 - 批量删除联系人")
    @ApiOperation("批量删除联系人")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "联系人ID集合", required = true) List<Long> idList) {
        userContactService.deleteBatchByids(idList);
        return Result.success("删除联系人成功！");
    }
    @ApiOperation("根据名称获取客户信息")
    @PostMapping("/selectName")
    public Result getUserContactByName(@RequestBody UserContactNameVo userContactNameVo) {
        return Result.success("获取客户账号信息成功").data("row", userService.getUserContactByName(userContactNameVo.getName()));
    }

}
